#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2021-9-6 10:24

from review.service.apis.wework_api import WeWork


class CorpTag(WeWork):
    """企业客户标签涉及的接口"""

    def get_corp_tag_list(self, tag_id=None, group_id=None):
        """获取企业标签库"""
        get_corp_tag_list_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list"
        data = {
            'method': "post",
            'url': get_corp_tag_list_url,
            'params': {"access_token": self.access_token},
            'json': {
                "group_id": group_id,
                "tag_id": tag_id
            },
        }
        return self.request(data)

    def add_corp_tag(self, tag_list, group_name,**kwargs):
        """添加企业客户标签"""
        if 'json' in kwargs:
            json_data = kwargs['json']
        else:
            json_data = {
                "group_name": group_name,
                "tag": tag_list,
            }
        add_corp_tag_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag"
        data = {
            'method': "post",
            'url': add_corp_tag_url,
            'params': {"access_token": self.access_token},
            'json': json_data,
        }
        return self.request(data)

    def edit_corp_tag(self, tag_id):
        """编辑企业客户标签"""
        pass

    def del_corp_tag(self, tag_id_list=None, group_id_list=None):
        """
        删除企业客户标签
        :param tag_id_list:
        :param group_id_list:
        :return:
        """
        del_corp_tag_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag"
        data = {
            'method': "post",
            'url': del_corp_tag_url,
            'params': {"access_token": self.access_token},
            'json': {
                "tag_id": [
                    tag_id_list,
                ],
                "group_id": [
                    group_id_list,
                ]
            },
        }
        return self.request(data)

    def clear(self):
        """清理数据"""
        r_list = self.get_corp_tag_list()
        tag_id_list = [tag['id'] for group in r_list.json()['tag_group'] for tag in group['tag']]
        r_delete = self.del_corp_tag(tag_id_list)
        return r_delete
